package com.aivin.lib_widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.LinearLayout;

import com.aivin.lib_base.tool.WkLog;


public class WkExpandLayout extends LinearLayout {
    private View layoutView;
    private int viewHeight;
    /**
     * 折叠后还能显示的高度
     */
    private int expandHeight =200;
    private boolean isExpand = true;
    private long animationDuration =300;
    private boolean isAnimationRunning =false;

    public WkExpandLayout(Context context) {
        this(context, null);
    }

    public WkExpandLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WkExpandLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        layoutView = this;
        layoutView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                layoutView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                layoutView.measure(View.MeasureSpec.makeMeasureSpec(0,   View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0,   View.MeasureSpec.UNSPECIFIED));
                viewHeight = layoutView.getMeasuredHeight();
                setViewHeight(layoutView, isExpand ? viewHeight : expandHeight); //初始化
            }
        });
    }

    /**
     * 初始化 配置。
     * @param isExpand  是否打开
     * @param expandHeight  折叠后还能显示的高度
     */
    public void initExpandViewConfig(final boolean isExpand , final int expandHeight ,int animationDuration) {
        this.isExpand = isExpand;
        this.expandHeight = expandHeight;
        this.animationDuration=animationDuration ;
    }


    private void setViewHeight(View view, int height) {
        if(height<=0){
            height = 1 ;
            view.setVisibility(GONE);
        }else{
            view.setVisibility(VISIBLE);
        }
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.height = height;
        //view.requestLayout();
        view.setLayoutParams(params);
    }

    /*
    * 开启动画。
    * @withScale 是否伴随开启缩放动画
     */
    public boolean toggleExpand( boolean withScale) {
        if (isAnimationRunning) {
            return  isExpand;
        }
        isExpand = !isExpand;
        animateToggle(animationDuration  ,withScale);
        return  isExpand;
    }


    /**
     * 切换动画实现 。属性动画 。
     */
    private void animateToggle(long animationDuration ,boolean withScale) {
        ValueAnimator animation= isExpand ?
                ValueAnimator.ofFloat(expandHeight, viewHeight) :
                ValueAnimator.ofFloat(viewHeight, expandHeight);

        animation.setDuration(animationDuration);
        animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) (float) animation.getAnimatedValue();
                setViewHeight(layoutView, value); // 动画更新过程中，动态设置空间高度
            }
        });

        animation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                isAnimationRunning = true;
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
                isAnimationRunning = false;
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                isAnimationRunning = false;
                animation.cancel();
            }
        });
        animation.start();
        if(withScale){
            scaleView(this ,isExpand) ;
        }
    }

    /**
     * 缩放动画
     */
    private void scaleView(View view  , boolean isToOpenView){
        Animation scaleAnimation = isToOpenView ?  new ScaleAnimation(
                1, 1, // 从A尺寸缩放到B尺寸
                0, 1,
                Animation.RELATIVE_TO_SELF,
                0f, // 缩放的轴点坐标
                Animation.RELATIVE_TO_SELF,
                0f)
                :
                 new ScaleAnimation(
                1, 1,
                1, 0,
                Animation.RELATIVE_TO_SELF,
                0f,
                Animation.RELATIVE_TO_SELF,
                0f);


        scaleAnimation.setDuration(animationDuration);
        scaleAnimation.setRepeatCount(0);
        scaleAnimation.setFillAfter(true);
        view.startAnimation(scaleAnimation);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        //执行动画的过程中屏蔽事件传递
        return isAnimationRunning;
    }



}
